import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/login.vue'
// import Home from '../components/home.vue'
// import Welcome from '../components/welcome.vue'
// import Breadcurmb from '../components/public/Breadcurmb.vue'

// import Users from '../components/users/users.vue'
// import Rights from '../components/power/rights.vue'
// import Roles from '../components/power/roles.vue'

// import Cate from '../components/goods/Cate.vue'
// import Params from '../components/goods/params.vue'

// import Goods from '../components/goods/goods.vue'
// import Add from '../components/goods/Add.vue'

// import Order from '../components/order/order.vue'
// import Report from '../components/report/report.vue'

const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/welcome.vue')
const Breadcurmb = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/public/breadcurmb.vue')

const Users = () => import(/* webpackChunkName: "Users" */ '../components/users/users.vue')
const Rights = () => import(/* webpackChunkName: "Users" */ '../components/power/rights.vue')
const Roles = () => import(/* webpackChunkName: "Users" */ '../components/power/roles.vue')

const Cate = () => import(/* webpackChunkName: "goods_1" */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "goods_1" */ '../components/goods/params.vue')

const Goods = () => import(/* webpackChunkName: "goods_2" */ '../components/goods/goods.vue')
const Add = () => import(/* webpackChunkName: "goods_2" */ '../components/goods/Add.vue')
const Order = () => import(/* webpackChunkName: "order_report" */ '../components/order/order.vue')
const Report = () => import(/* webpackChunkName: "order_report" */ '../components/report/report.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      // { path: '/welcome', component: { title: Breadcurmb, default: Welcome } },
      { path: '/welcome', component: Welcome },
      { path: '/users', components: { title: Breadcurmb, default: Users } },
      { path: '/rights', components: { title: Breadcurmb, default: Rights } },
      { path: '/roles', components: { title: Breadcurmb, default: Roles } },
      { path: '/categories', components: { title: Breadcurmb, default: Cate } },
      { path: '/params', components: { title: Breadcurmb, default: Params } },
      { path: '/goods', components: { title: Breadcurmb, default: Goods } },
      { path: '/goods/add', component: Add },
      { path: '/goods/eidt', name:'edit', component: Add },
      { path: '/orders', components: { title: Breadcurmb, default: Order } },
      { path: '/reports', components: { title: Breadcurmb, default: Report } }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, form, next) => {
  if (to.path === '/login') {
    return next()
  } else {
    // 检查是否含有token值，如空返回/login
    const usrToken = window.sessionStorage.getItem('token')
    if (!usrToken) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
